package com.kanshu.client.ui.base;

import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.CallSuper;
import android.widget.Toast;


import com.kanshu.client.widget.dialog.DialogManager;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity {

    private DialogManager dialogManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(provideLayoutResId());
        ButterKnife.bind(this);
        init();
    }

    /**
     * 提供title的资源
     *
     * @return
     */
    protected abstract String provideBarTitle();


    /**
     * 提供布局资源文件
     *
     * @return {@linkplain com.kanshu.client.R.layout#*}
     */
    protected abstract int provideLayoutResId();


    @CallSuper
    protected void init() {
        dialogManager = new DialogManager(this);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle(provideBarTitle());
        }

    }


    protected DialogManager getDialogManager() {
        return dialogManager;
    }

    protected void showPromptDialog(String title, String message) {
        dialogManager.createCommonDialog(title, message, "确定", null, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        }, null).show();
    }

    protected void showToastShort(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        dialogManager.dismiss();
    }
}
